package com.jvyou.task;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

@Component
public class MyScheduledTasks {

    private final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Scheduled(fixedRate = 10000) // 每60秒执行一次
    public void performTaskWithFixedRate() {
        System.out.println("Task executed at fixed rate: " + sdf.format(new Date()));
    }

    @Scheduled(fixedDelay = 5000) // 上一个任务完成后5秒执行
    public void performTaskWithFixedDelay() {
        System.out.println("Task executed with fixed delay: " + sdf.format(new Date()));
    }

    @Scheduled(cron = "*/5 * * * * ?") // 每小时的第0分钟执行
    public void performTaskWithCronExpression() {
        System.out.println("Task executed with cron expression: " + sdf.format(new Date()));
    }
}